package com.xuanxuan.socketdemo.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.xuanxuan.socketdemo.R;
import com.xuanxuan.socketdemo.adapter.TerminalAdapter;
import com.ywicc.commlibs.socket.ServerService;
import com.ywicc.commlibs.socket.SocketUtils;
import com.ywicc.commlibs.socket.WifiUtils;

import java.util.ArrayList;
import java.util.Arrays;

public class ServerActivity extends AppCompatActivity implements View.OnClickListener, ServerService.ServerServiceImpl {

    private Intent intent;
    private ServerService service;
    private TerminalAdapter terminalAdapter;

    private int number = 0;

    @SuppressLint({"SetTextI18n", "WrongConstant"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.server_activity);

        Button btn_start = findViewById(R.id.btn_server_start);
        Button btn_stop = findViewById(R.id.btn_server_stop);
        Button btn_bind = findViewById(R.id.btn_server_bind);
        Button btn_unbind = findViewById(R.id.btn_server_unbind);

        TextView tv_ip = findViewById(R.id.tv_server_ip);
        TextView tv_port = findViewById(R.id.tv_server_port);

        RecyclerView recyclerView = findViewById(R.id.rv_server);

        ArrayList<String> data = new ArrayList<String>();

        terminalAdapter = new TerminalAdapter(data);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(terminalAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
        btn_bind.setOnClickListener(this);
        btn_unbind.setOnClickListener(this);

        intent = new Intent(ServerActivity.this, ServerService.class);

        WifiUtils wifiUtils = new WifiUtils(ServerActivity.this);

        tv_ip.setText("本机IP: " + wifiUtils.getSelfIP());
        tv_port.setText("服务端口: " + SocketUtils.IPPort);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_server_start:
                startService(intent);
                terminalAdapter.addText("启动服务");
                break;
            case R.id.btn_server_stop:
                terminalAdapter.addText("关闭服务");
                stopService(intent);
                break;
            case R.id.btn_server_bind:
                terminalAdapter.addText("绑定服务");
                bindService(intent, connection, Context.BIND_AUTO_CREATE);
                break;
            case R.id.btn_server_unbind:
                terminalAdapter.addText("解绑服务");
                unbindService(connection);
                break;
        }
    }

    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            ServerService.ServerBinder binder = (ServerService.ServerBinder) iBinder;
            service = binder.getService();

            service.setServerServiceImpl(ServerActivity.this);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    public void callBackServerReceiveData(byte[] data) {
        if (data.length > 0) {
            terminalAdapter.addText("接收的数据为: " + Arrays.toString(data));
        }
    }
}